package cn.xianshu.org.controller;

import cn.xianshu.basic.exception.BasicException;
import cn.xianshu.basic.util.AxiosResult;
import cn.xianshu.basic.util.PageList;
import cn.xianshu.org.domain.Shop;
import cn.xianshu.org.query.ShopQuery;
import cn.xianshu.org.service.IShopService;
import cn.xianshu.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    /**
     * 注入service层对象
     */
    @Autowired
    private IShopService shopService;



    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id") Long id){
        return shopService.findById(id);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AxiosResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统繁忙，请稍后重试。。。。");
        }
    }

    @PatchMapping
    public AxiosResult Bydelete(@RequestBody List<Long> ids){
        try {
            shopService.Bydelete(ids);
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统繁忙，请稍后重试。。。。");
        }
    }

    /**
     * 添加修改
     * @param
     * @return
     */
    @PutMapping
    public AxiosResult addAndUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId()==null){
                shopService.insert(shop);
            } else {
                shopService.update(shop);
            }
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统繁忙，请稍后重试。。。。");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.pageQuery(query);
    }

    @GetMapping("/tree")
    public List<Shop> queryTree(){
        return shopService.queryTree();
    }


   @PostMapping("/settlement")
    public AxiosResult shopRegister(@RequestBody Shop shop){
        try {
            shopService.shopRegister(shop);
            return new AxiosResult();
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage(e.getMessage()); // 给用户看的
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统繁忙，请稍后重试。。。。");
        }
    }


}
